/**
 * 
 */
package jp.sourceforge.akjrcp.ui.param;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.sourceforge.akjrcp.ui.GenericParametersEventListener;
import jp.sourceforge.akjrcp.ui.GenericParametersOptionList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.widgets.Composite;

import com.akjava.vocaloidtools.wavtovsq.WavToVocaloidMidiViewer;

/*
 * NO GUI
 */
public class DefaultParamOptionEditor  implements GenericParametersOptionList, GenericParametersEventListener{
	public static Log log=LogFactory.getLog(DefaultParamOptionEditor.class);
			Map<String,String> parameterMap=new HashMap<String,String>();
			private Composite inputComposite;
			
			private boolean newMode=false;
			private boolean dirMode;

			private Composite basicComposite;
			public synchronized Map<String,String> getParameters(){
				updateParameters();//where shoud i call it.add GenericParametersEventListener?if speed is slow this make problem
				return parameterMap;
			}
			public  void updateParameters(){
				
				Map<String,String> parameterMap=new HashMap<String,String>();
				for (int i = 0; i < paramList.size(); i++) {
					ParamItem item=paramList.get(i);
					if(item.getValue().length()>0){
						parameterMap.put(item.getKey(), item.getValue());
					}
				}
				synchronized(this){
				this.parameterMap=parameterMap;
				}
			}
			
			//called when item selected
			public void setParameters(Map<String,String> map){
				for (int i = 0; i < paramList.size(); i++) {		
					ParamItem item=paramList.get(i);
					String key=item.getKey();
					log.info("key:"+key);
					//ignore when mode is new and about dir params
					if(!isIgnoreEditModeKey(key) || !isNewMode()){
					String value=(String) map.get(key);
					log.info("value:"+value);
					if(value!=null){
						//log.info(item.getKey()+","+value);
						item.setValue(value);
					}else{
						item.setValue("");
					}
					}
				}
			
			}

			//should overwrite if need.
			private boolean isIgnoreEditModeKey(String key){

				return false;
			}
			

			
			public void updateModified(boolean modified) {
				updateParameters();
			}
			public Composite getInputComposite() {
				return inputComposite;
			}
			public boolean isNewMode() {
				return newMode;
			}
			public void setNewMode(boolean newMode) {
				this.newMode = newMode;
			}
			

			
			

			public ParamItem getItem(String key) {
				for (int i = 0; i < paramList.size(); i++) {		
					ParamItem item=paramList.get(i);
					if(item.getKey().equals(key)){
						return item;
					}
				}
				return null;
			}

			public void setEditable(boolean b) {
				basicComposite.setEnabled(b);
				inputComposite.setEnabled(b);
			}
			public boolean isDirMode() {
				return dirMode;
			}
			public void setDirMode(boolean dirMode) {
				this.dirMode = dirMode;
			}
			
			

			public List<GenericParametersEventListener> eventList=new ArrayList<GenericParametersEventListener>();
			protected List<ParamItem> paramList=new ArrayList<ParamItem>();
			ItemModifyCheck itemModifyCheck=new ItemModifyCheck();
			public class ItemModifyCheck implements ParamItemActionListener{

				public void paramItemModified(ParamItem source) {
					boolean modified=false;
					for (int i = 0; i < paramList.size(); i++) {
						ParamItem item=paramList.get(i);
						if(item.isModified()){
							//log.info("modified:"+item.getKey());
							modified=true;
							break;
						}
						
					}
					
				
						fireModified(modified); 
					
				}
				
			}


			public void addParamList(ParamItem item){
				//for debug
				//log.trace("parameters.set(\""+item.getKey()+"\", \""+item.getValue()+"\");");
				
				paramList.add(item);
				item.setParamItemActionListener(itemModifyCheck);
				
			}

			public void addListener(GenericParametersEventListener listener){
				eventList.add(listener);
			}

			public void removeListener(GenericParametersEventListener listener){
				eventList.remove(listener);
			}
			private void fireModified(boolean bool){
				GenericParametersEventListener[] listeners=null;
				synchronized(this){
					 listeners=eventList.toArray(new GenericParametersEventListener[eventList.size()]);
				}
				for (int i = 0; i < listeners.length; i++) {
					listeners[i].updateModified(bool);
				}

			}
		

			protected IPreferenceStore preferenceStore;

			protected String preferenceKey;
			
			public IPreferenceStore getPreferenceStore() {
				return preferenceStore;
			}

			public void setPreferenceStore(IPreferenceStore preferenceStore) {
				this.preferenceStore = preferenceStore;
			}

			public String getPreferenceKey() {
				return preferenceKey;
			}

			public void setPreferenceKey(String preferenceKey) {
				this.preferenceKey = preferenceKey;
			}

	

			
			

			
		}